package com.topfinance.frame.util;

import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;

import javax.persistence.Table;
import javax.persistence.Column;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.topfinance.cnaps2.ebo.msg.Header;
import com.topfinance.payment.ebo.TCfgNodeEbo;

public class StringUtil {
	private static final Log log = LogFactory.getLog(StringUtil.class);

	public static boolean isNullInteger(Integer val) {
		if (val == null)
			return true;
		if(val.intValue() < 0)
			return true;
		return false;
	}

	public static boolean isNullStr(String src) {
		if (src == null)
			return true;
		if(src.trim().length() == 0)
			return true;
		if("null".equals(src))
			return true;
		return false;
	}

	public static boolean isNotNullStr(String src) {
		return !isNullStr(src);
	}

	public static String convertStr(String src) {
		if (src == null)
			return "";
		else
			return src;
	}

	public static String convertDate(Timestamp date) {
		if (date == null)
			return "";
		SimpleDateFormat formatter = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss");
	    return formatter.format(date);
	}

	public static String convertTimestamp(Timestamp date) {
		if (date == null)
			return "";
		SimpleDateFormat formatter = new SimpleDateFormat ("yyyy-MM-dd-HH:mm:ss:SSS");
	    return formatter.format(date);
	}

	public static String convertShortTimes(Timestamp date) {
		if (date == null)
			return "";
		SimpleDateFormat formatter = new SimpleDateFormat ("HH:mm:ss:SSS");
	    return formatter.format(date);
	}
	
	public static String mapObject(Object bean) {
		StringBuffer buf = new StringBuffer();
		PropertyDescriptor[] descr = PropertyUtils.getPropertyDescriptors(bean);
		// Map props = new HashMap();
		buf.append("{\n");
		for (int i = 0; i < descr.length; i++) {
			PropertyDescriptor d = descr[i];
			if (d.getReadMethod() == null)
				continue;
			try {
				// props.put(d.getName(), PropertyUtils.getProperty(bean,
				// d.getName()));
				buf.append("    [" + d.getName() + "]");
				buf.append(PropertyUtils.getProperty(bean, d.getName()));
				buf.append("\n");
			} catch (Exception e) {
			}
		}
		buf.append("}");
		return buf.toString();
	}

	public static List mapClass(Class bean) {
		List list = new ArrayList();
		StringBuffer buf = new StringBuffer();
		PropertyDescriptor[] descr = PropertyUtils.getPropertyDescriptors(bean);
		for (int i = 0; i < descr.length; i++) {
			PropertyDescriptor d = descr[i];
			if (d.getReadMethod() == null)
				continue;
			try {
				if (!d.getName().startsWith("class")) {
					Header h = new Header();
					h.setName(d.getName());
					list.add(h);
				}
			} catch (Exception e) {
			}
		}
		return list;
	}

	public static Hashtable mapPropsColumn(Method[] methods) {
		Hashtable h = new Hashtable();
		// log.debug("[mapPropsColumn]" + methods.length);
		for (Method method : methods) {
			if (method.getName().startsWith("get")) {
				Column column = method.getAnnotation(Column.class);
				String methodName = method.getName();
				methodName = methodName.substring(3);
				char initChar = (char) (methodName.charAt(0) + 32);
				methodName = new String(initChar + methodName.substring(1));
				if (column != null) {
					if (column.name() != null) {
						// log.debug("[" + methodName.toUpperCase() + "]" +
						// column.name());
						h.put(methodName.toUpperCase(), column.name());
					} else {
						// 没有设置，暂作默认处理
						h.put(methodName.toUpperCase(), methodName);
						// log.debug("[" + methodName.toUpperCase() + "]" +
						// methodName.toUpperCase());
					}
				}
			}
		}
		return h;
	}

	public static String mapTableName(Class<TCfgNodeEbo> bean) {
		if (bean.isAnnotationPresent(Table.class)) {
			Table tAnnot = bean.getAnnotation(Table.class);
			return tAnnot.name();
		}
		return null;
	}

	public static void main(String arg[]) {
		// Integer i = new Integer(100);
		// SysOrgDto ot = new SysOrgDto();
		// ot.setPID(100);
		// System.out.println(StringUtil.mapClass(SysOrgDto.class));

		// List l = StringUtil.mapClass(ConfNodeEbo.class);
		// for (Object h : l) {
		// System.out.println(((Header) h).getName());
		// }

		// StringUtil.mapAnnotation(ConfNodeEbo.class);
		System.out.println("[TableName]" + TCfgNodeEbo.class.getAnnotation(Table.class).name());
		// StringUtil.mapAnnotation(ConfOperationEbo.class.getMethods());
	}
}
